package com.slf.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slf.common.utils.Result;
import org.apache.commons.lang3.StringUtils;
import com.slf.flow.core.model.Function;
import com.slf.flow.core.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 18812
 */
@RestController
@RequestMapping("/function")
public class FunctionController {

    @Autowired
    private FunctionService functionService;

    @GetMapping("/list")
    public Result<IPage<Function>> list(@RequestParam(name = "page",defaultValue = "1")Integer page, @RequestParam(name = "limit",defaultValue = "1")Integer size, String name) {
        Result<IPage<Function>> result = new Result<>();
        QueryWrapper<Function> select = new QueryWrapper<Function>().select("id", "name", "parameter", "create_date");
        if(StringUtils.isNotBlank(name)){
            select.like("name",name);
        }
        select.orderByDesc("create_date");
        Page<Function> functionPage = functionService.page(new Page<Function>(page, size), select);
        return result.ok(functionPage);
    }

    @PostMapping("/save")
    public Result<String> save(@RequestBody Function function){

        return new Result<String>().ok(functionService.saveFunction(function));
    }

    @PutMapping("/save")
    public Result<String> update(@RequestBody Function function){

        return new Result<String>().ok(functionService.saveFunction(function));
    }

    @GetMapping("/get/{id}")
    public Result<Function> get(@PathVariable("id") String id){
        return new Result<Function>().ok(functionService.getById(id));
    }

    @DeleteMapping("/remove/{id}")
    public Result<Boolean> remove(@PathVariable("id")String id){
        boolean b = functionService.removeById(id);
        return new Result<Boolean>().ok(b);
    }
}
